clear all
set more off
set matsize 10000
set maxvar 10000
adopath + ../code/gslab_tools/

preliminaries, doutf(../derived/Antidepressants)
graph set window fontface "Times New Roman"


/**********
* Import Study Characteristics *
***********/

import delimited "../raw/Antidepressents/study_characteristics.csv", clear

replace year = "unpublished" if year=="unpublis" | year=="Unpubli"
replace year = "" if year=="hed" | year=="shed"

gen count = year!="" & year[_n-1]==""
gen study_no = sum(count)
drop if study_no==0

bys study_no: gen num_obs=_N
gen studyname_temp = studyname[_n] + " " + studyname[_n+1]
replace studyname_temp = studyname_temp + " " + studyname[_n+2] if num_obs>=3
replace studyname_temp = studyname_temp + " " + studyname[_n+3] if num_obs>=4
replace studyname_temp = studyname_temp + " " + studyname[_n+4] if num_obs>=5
replace studyname_temp = studyname_temp + " " + studyname[_n+5] if num_obs>=6
replace studyname_temp = studyname_temp + " " + studyname[_n+6] if num_obs>=7
gen multisinglecenter_temp = multisinglecenter[_n]+multisinglecenter[_n+1]
gen recruitment_temp = recruitment[_n]+" "+recruitment[_n+1]
gen treatmentsetting_temp = treatmentsetting[_n]+treatmentsetting[_n+1]+" "+treatmentsetting[_n+2]
gen patientstatus_temp = patientstatus[_n]+" "+patientstatus[_n+1]

foreach var in studyname multisinglecenter recruitment treatmentsetting patientstatus  {
	replace `var' = `var'_temp if count==1
}

drop if drug =="" | drug=="ne" | drug=="e" | drug=="ran"
drop count *temp
foreach var in studyname year noofarms nooftotal lengthof diagnostic recruitment multisingle ///
	treatmentsetting patientstatus placeborun rescue {
	bys study_no: replace `var'=`var'[_n-1] if study_no==study_no[_n-1]
}
replace studyname = strtrim(studyname)

foreach var in recruitment multisingle treatmentsetting patientstatus {
	replace `var'=strtrim(`var')
}

replace recruitment = "Unspecified" if recruitment =="Unspecified " | recruitment =="*"
replace recruitment = "Cross-Continental" if substr(recruitment,1,5)=="Cross"
replace recruitment = "North America" if recruitment=="North"

replace treatmentsetting = "Secondary / Tertiary" if substr(treatmentsetting,1,6)=="Second"
replace treatmentsetting = "Unclear" if substr(treatmentsetting,1,2)=="Un" | ///
	treatmentsetting =="*" | treatmentsetting=="/Unclear" | substr(treatmentsetting,1,6)=="Unclear" | ///
	treatmentsetting=="nclear" | substr(treatmentsetting,1,5)=="Other"

replace patientstatus = "Inpatient" if substr(patientstatus,1,5)=="Inpat"
replace patientstatus = "Outpatient" if substr(patientstatus,1,5)=="Outpa"
replace patientstatus ="Unclear" if substr(patientstatus,1,5)=="Other" | ////
	patientstatus=="Other/Unclea r" | substr(patientstatus,1,7)=="Unclear" | ///
	patientstatus=="*"
	
replace multisinglecenter = "Multi" if substr(multi,1,5)=="Multi"
replace multisinglecenter = "Single" if substr(multi,1,6)=="Single"
replace multisinglecenter = "Unclear" if multi=="*"

replace placeborun = "Yes" if placeborun=="yes"
replace placeborun = "Unclear" if placeborun=="*"

strip studyname, of(" ","-","(",")","&","'","/,"_"","\") gen(study_merge)
replace study_merge=subinstr(study_merge,char(144),"",.)
replace study_merge=subinstr(study_merge,char(194),"",.)
replace study_merge=substr(study_merge,1,20)
replace study_merge=lower(study_merge)
replace study_merge="staner1995063" if study_merge=="staner199563"

replace study_merge="march1990" if study_merge=="marchstudy1990"
replace study_merge="khan2011clda07dp02fd" if study_merge=="khan2011clda07dp02nc"
replace study_merge="kennedy2006" if strpos(study_merge,"kennedy2008")==1
replace study_merge="ekselius1997" if study_merge=="ekslius1997"
replace study_merge="e1569" if study_merge=="e159"
replace study_merge="dunbar1993bclaghorn1" if study_merge=="dunbar1993bpar02002f"

drop if studyname==""
drop if year==""
replace drug=strtrim(drug)
replace drug="clomipramine" if drug=="clomipramin"
replace drug="levomilnacipran" if drug=="levomilnacip"
replace drug="desvenlafaxine" if drug=="desvenlafaxi"
replace drug="placebo" if drug=="Placebo"
replace drug="venlafaxine XR" if norandomized=="97" & study_merge=="cunningham1997venxr2"
replace baselineseverityscale="HAMD21" if drug =="placebo" & studyname=="Kramer 1998"
gen dose_min = substr(doseminmax,1,strpos(doseminmax,"-")-1)
destring dose_min, ignore(" " "*") replace
destring nooftotal, replace force
replace dose_min = . if drug=="placebo"
duplicates drop study_merge drug dose_min, force

*Fix three errors, noted in paper text
*Funded by Pfizer
replace sponsored = "Yes" if study_merge=="abergwisted2000" & drug == "sertraline" 
*AstraZenca, discontinued in 2003
replace sponsored = "Yes" if study_merge=="amsterdam1986" & drug == "amitriptyline"
*Pfizer
replace sponsored = "Yes" if study_merge=="lydiard1997" & drug == "sertraline"

rename sponsored sponsor_str
replace sponsor_str = "No" if sponsor_str == "no"
gen sponsor = 1 if sponsor_str == "Yes" | sponsor_str=="Unclear"
replace sponsor =0 if sponsor_str=="No"
gen sponsor_remove_coi = sponsor_str=="Yes"

save ../derived/Antidepressants/studychars.dta, replace

/**********
* Import Study Outcomes *
***********/

import delimited "../raw/Antidepressents/Cipriani et al_GRISELDA_Lancet 2018_Open data.csv", varnames(3) clear 
drop if studyid==""
drop if drug==""
replace studyid=strtrim(studyid)
strip studyid, of(" ","-","(",")","&","'","/,"_"","\") gen(study_merge)
replace study_merge=subinstr(study_merge,char(191),"",.)
replace study_merge=subinstr(study_merge,char(195),"",.)
replace study_merge=substr(study_merge,1,20)
replace study_merge=lower(study_merge)
replace study_merge = subinstr(study_merge,"fda","",.) if strpos(study_merge,"fda2")==1
replace study_merge="kennedy2006" if strpos(study_merge,"kennedy2006")==1
replace study_merge="march1990" if study_merge=="mar90"

rename year_published year
replace year = "unpublished" if year=="unpublis" | year=="Unpubli"
replace year = "" if year=="hed" | year=="shed"
replace drug="placebo" if drug=="Placebo"

gen dose_temp = substr(drug,strpos(drug," "),length(drug)-1-strpos(drug," "))
replace dose_temp=subinstr(dose_temp,"XR ","",.)
replace dose_temp=subinstr(dose_temp,"IR ","",.)
split dose_temp, p("-")
replace dose_temp2 = dose_temp1 if dose_temp2 ==""
rename dose_temp1 dose_min
destring dose_min, replace force
gen drug_temp=substr(drug,1,strpos(drug," ")-1)
replace drug=drug_temp if drug_temp!=""
drop *temp

*Fix one duplicate for venlafaxine and venlafaxine XL
replace drug="venlafaxine XR" if no_randomised=="97" & study_merge=="cunningham1997venxr2"
replace scale="HAMD21" if drug =="placebo" & studyid=="Kramer1998"
rename mean mean_baseline
rename v18 mean_post
duplicates drop study_merge drug dose_min, force

save ../derived/Antidepressants/studyresults.dta, replace


/**********
* Merge Study Data and Characteristics
***********/

*First merge by studyname, drug, and dosage
use ../derived/Antidepressants/studyresults.dta, clear
merge 1:m study_merge drug dose_min using ../derived/Antidepressants/studychars.dta, assert(1 2 3) ///
	keep(3) nogen
save ../derived/Antidepressants/merge_dosage.dta, replace

*Then merge by studyname and drug
use ../derived/Antidepressants/studychars.dta, clear
merge 1:m study_merge drug dose_min using ../derived/Antidepressants/merge_dosage.dta, ///
	assert(1 3) keep(1) keepusing(studyid) nogen
save ../derived/Antidepressants/studychars_nodosage.dta, replace

use ../derived/Antidepressants/studyresults.dta, clear
merge 1:m study_merge drug dose_min using ../derived/Antidepressants/merge_dosage.dta, ///
	assert(1 3) keep(1) keepusing(studyid) nogen
merge 1:1 study_merge drug using ../derived/Antidepressants/studychars_nodosage.dta, ///
     assert(1 2 3 4 5) keep(3 4 5) nogen update
append using ../derived/Antidepressants/merge_dosage.dta

replace drug = "venlafaxine" if drug=="venlafaxine XR"
save ../derived/Antidepressants/merged_char_results.dta, replace


/**********
* Import Data on Additional Antidepressant Arms
***********/

import delimited "../raw/Antidepressents/study_characteristics_addl.csv", clear

replace year = "unpublished" if year=="unpublis" | year=="Unpubli"
replace year = "" if year=="hed" | year=="shed"

gen count = year!="" & year[_n-1]==""
gen study_no = sum(count)
drop if study_no==0

bys study_no: gen num_obs=_N
gen studyname_temp = studyname[_n] + " " + studyname[_n+1]
replace studyname_temp = studyname_temp + " " + studyname[_n+2] if num_obs>=3
replace studyname_temp = studyname_temp + " " + studyname[_n+3] if num_obs>=4
replace studyname_temp = studyname_temp + " " + studyname[_n+4] if num_obs>=5
replace studyname_temp = studyname_temp + " " + studyname[_n+5] if num_obs>=6
replace studyname  = studyname_temp if count==1

drop if drug =="" 
drop count *temp
foreach var in studyname year noofarms nooftotal {
	bys study_no: replace `var'=`var'[_n-1] if study_no==study_no[_n-1]
}
replace studyname = strtrim(studyname)
drop study_no num_obs
gen addl_drug =1
rename sponsored sponsor_str
replace sponsor_str = "No" if sponsor_str == "no"
gen sponsor = 1 if sponsor_str == "Yes" | sponsor_str=="Unclear"
replace sponsor =0 if sponsor_str=="No"
gen sponsor_remove_coi = sponsor_str=="Yes"
save ../derived/Antidepressants/addl_arms.dta, replace


/**********
* Merge in additional arms
*********/
use ../derived/Antidepressants/merged_char_results.dta, clear

keep studyname study_no studyid scale placeborun treatment patientstatus recruitment multi lengthoftrial
duplicates drop
merge 1:m studyname using ../derived/Antidepressants/addl_arms.dta, assert(1 3) keep(3) nogen
save ../derived/Antidepressants/addl_arms_chars.dta, replace

use ../derived/Antidepressants/merged_char_results.dta, clear
gen addl_drug = 0
append using ../derived/Antidepressants/addl_arms_chars.dta
save ../derived/Antidepressants/merged_char_results.dta, replace

/**********
* Create Effect Varaiables
***********/

use ../derived/Antidepressants/merged_char_results.dta, clear

destring year, replace force
*Approved in Europe in 2009; not yet approved in the USA
gen approve_year = . if drug=="agomelatine"
replace approve_year = 1961 if drug=="amitriptyline"
replace approve_year = 1985 if drug=="bupropion"
replace approve_year = 1998 if drug=="citalopram"
replace approve_year = 1989 if drug=="clomipramine"
replace approve_year = 2007 if drug=="desvenlafaxine"
*Approved for depression in 2004, for anxiety in 2007
replace approve_year = 2004 if drug=="duloxetine"
replace approve_year = 2002 if drug=="escitalopram"
replace approve_year = 1987 if drug=="fluoxetine"
replace approve_year = 1994 if drug=="fluvoxamine"
replace approve_year = 2013 if drug=="levomilnacipran"
*Approved in 2009 but only for fibromyglia
replace approve_year = . if drug=="milnacipran"
replace approve_year = 1994 if drug=="mirtazapine"
replace approve_year = 1994 if drug=="nefazodone"
replace approve_year = 1992 if drug=="paroxetine"
*Approved in Europe in 1997; not yet approved in the USA
replace approve_year = . if drug=="reboxetine"
replace approve_year = 1991 if drug=="sertraline"
replace approve_year = 1981 if drug=="trazodone"
replace approve_year = 1993 if drug=="venlafaxine" | drug=="venlafaxine XR"
replace approve_year = 2011 if drug=="vilazodone"
replace approve_year = 2013 if drug=="vortioxetine"
replace approve_year= . if drug=="placebo"

gen patent_year = . if drug=="agomelatine"
replace patent_year = 1980 if drug=="amitriptyline"
replace patent_year = 2006 if drug=="bupropion"
replace patent_year = 2003 if drug=="citalopram"
replace patent_year = 1983 if drug=="clomipramine"
replace patent_year = 2022 if drug=="desvenlafaxine"
replace patent_year = 2013 if drug=="duloxetine"
replace patent_year = 2012 if drug=="escitalopram"
replace patent_year = 2001  if drug=="fluoxetine"
replace patent_year = 2000 if drug=="fluvoxamine"
replace patent_year = 2023  if drug=="levomilnacipran"
replace patent_year = . if drug=="milnacipran"
replace patent_year = 2004 if drug=="mirtazapine"
replace patent_year = 2003  if drug=="nefazodone"
replace patent_year = 2003 if drug=="paroxetine"
replace patent_year = . if drug=="reboxetine"
replace patent_year = 2006 if drug=="sertraline"
replace patent_year = 1990  if drug=="trazodone"
replace patent_year = 2008  if drug=="venlafaxine" | drug=="venlafaxine XR"
replace patent_year = 2022  if drug=="vilazodone"
replace patent_year = 2027 if drug=="vortioxetine"
replace patent_year= . if drug=="placebo"

gen drug_type = "Atypical" if drug=="agomelatine"
replace drug_type = "Tricyclic" if drug=="amitriptyline"
replace drug_type = "Atypical" if drug=="bupropion"
replace drug_type = "SSRI" if drug=="citalopram"
replace drug_type = "Atypical" if drug=="clomipramine"
replace drug_type = "SNRI" if drug=="desvenlafaxine"
replace drug_type = "SNRI" if drug=="duloxetine"
replace drug_type = "SSRI" if drug=="escitalopram"
replace drug_type = "SSRI"  if drug=="fluoxetine"
replace drug_type = "SSRI" if drug=="fluvoxamine"
replace drug_type = "SNRI"  if drug=="levomilnacipran"
replace drug_type = "SNRI" if drug=="milnacipran"
replace drug_type = "Atypical" if drug=="mirtazapine"
replace drug_type = "Atypical"  if drug=="nefazodone"
replace drug_type = "SSRI" if drug=="paroxetine"
replace drug_type = "Atypical" if drug=="reboxetine"
replace drug_type = "SSRI" if drug=="sertraline"
replace drug_type = "Atypical"  if drug=="trazodone"
replace drug_type = "SNRI"  if drug=="venlafaxine" | drug=="venlafaxine XR"
replace drug_type = "SSRI"  if drug=="vilazodone"
replace drug_type = "Atypical" if drug=="vortioxetine"
replace drug_type= "Placebo" if drug=="placebo"

gen firm = "Servier" if drug=="agomelatine"
replace firm = "AstraZenca" if drug=="amitriptyline"
replace firm = "Burroughs" if drug=="bupropion" & year<= 1994
replace firm = "Glaxo Wellcome" if drug=="bupropion" & year>=1995 & year<2000
replace firm = "GSK" if drug=="bupropion" & year>=2000
replace firm = "Lundbeck/Forest" if drug=="citalopram"
replace firm = "Ciba_Beigy, now Novartis" if drug=="clomipramine"
replace firm = "Wyeth" if drug=="desvenlafaxine" & year<=2008
replace firm = "Pfizer" if drug=="desvenlafaxine" & year>=2009
replace firm = "Eli Lilly" if drug=="duloxetine"
replace firm = "Lundbeck/Forest" if drug=="escitalopram"
replace firm = "Eli Lilly"  if drug=="fluoxetine"
replace firm = "Solvay, Abbott" if drug=="fluvoxamine"
replace firm = "Lundbeck/Forest"  if drug=="levomilnacipran"
replace firm = "Pierre Fabre" if drug=="milnacipran"
replace firm = "Organon" if drug=="mirtazapine"
replace firm = "Bristol-Myers Squibb"  if drug=="nefazodone"
replace firm = "GSK" if drug=="paroxetine"
replace firm = "Pfizer" if drug=="reboxetine"
replace firm = "Pfizer" if drug=="sertraline"
replace firm = "Angelli"  if drug=="trazodone"
replace firm = "Wyeth"  if drug=="venlafaxine" | drug=="venlafaxine XR"
replace firm = "Lundbeck/Forest"  if drug=="vilazodone"
replace firm = "Lundbeck/Forest" if drug=="vortioxetine"
replace firm= "Placebo" if drug=="placebo"

*Generate effect sizes
destring mean_baseline, replace force
destring mean_post, replace force
gen effect = mean_post - mean_baseline if mean_post>=0
replace effect = mean_post if mean_post<0
replace effect = -1*effect

gen placebo_control_temp = drug=="placebo"
bys study_no: egen placebo_control = max(placebo_control_temp)
gen effect_temp = effect if drug=="placebo" & placebo_control==1
bys study_no (effect): replace effect_temp = effect ///
	if _n==1 & placebo_control==0
bys study_no: egen placebo_effect=min(effect_temp)
gen effect_relative = effect - placebo_effect

gen effect_perc = effect / mean_baseline
gen effect_perc_temp = effect_perc if drug=="placebo" & placebo_control==1
bys study_no (effect_perc): replace effect_perc_temp = effect_perc ///
	if _n==1 & placebo_control==0
bys study_no: egen placebo_effect_perc=min(effect_perc_temp)
gen effect_perc_relative = effect_perc - placebo_effect_perc
sort study_no drug

*Generate response shares
destring no_randomised , replace force
destring responders , replace force
destring sd, replace force

gen share_respond = responders / no_randomised
gen placebo_share_temp = share_respond if drug=="placebo" & placebo_control==1
bys study_no (share_respond): replace placebo_share_temp = share_respond ///
	if _n==1 & placebo_control==0
bys study_no: egen placebo_share=min(placebo_share_temp)
gen share_respond_relative = share_respond - placebo_share
drop *temp placebo_effect placebo_share

bys study_no (effect_perc): gen best_effect_perc=_n==_N
bys study_no (share_respond): gen best_share_respond=_n==_N

*Order drugs correctly
gen drug_order="zplacebo" if drug=="placebo"
replace drug_order = "a" if drug!="placebo"
gen neg_share_respond = 1-share_respond
gen neg_effect_perc = 1- effect_perc
bys study_no (drug_order neg_share_respond): gen num_drug=_n
bys study_no (drug_order neg_effect_perc): gen num_drug_perc=_n
drop drug_order neg_share_respond neg_effect_perc

*Generate standardized outcomes
summ share_respond
gen stdz_share_respond = (share_respond - `r(mean)') / `r(sd)'

gen placebo_share_temp = stdz_share_respond if drug=="placebo" & placebo_control==1
bys study_no (stdz_share_respond): replace placebo_share_temp = stdz_share_respond ///
	if _n==1 & placebo_control==0
bys study_no: egen placebo_share=min(placebo_share_temp)
gen stdz_share_respond_relative = stdz_share_respond - placebo_share
drop *temp placebo_share

summ effect_perc_relative
gen stdz_effect_perc_relative = (effect_perc_relative - `r(mean)') / `r(sd)'
save ../derived/Antidepressants/antidepressant.dta, replace

********
*Generate significance score for response
**********

use ../derived/Antidepressants/antidepressant.dta, clear
keep study_no studyname drug num_drug responders no_randomised mean_baseline effect sd share_respond
drop if share_respond==. & effect==.
gen endpoint = mean_baseline - effect
reshape wide drug responders no_random mean_base effect sd endpoint ///
	share_respond, i(study_no) j(num_drug)

gen share_overall = (responders1 + responders2) / (no_randomised1 + no_randomised2) if drug3==""
gen z_overall2_p1 = (share_respond1 - share_respond2) / sqrt(share_overall*(1-share_overall)* ///
	((1/no_randomised1) + (1/no_randomised2)))
gen z_overall2_p2 = (share_respond2 - share_respond1) / sqrt(share_overall*(1-share_overall)* ///
	((1/no_randomised1) + (1/no_randomised2)))

gen share_overall3_p1 = (responders1 + responders3) / (no_randomised1 + no_randomised3) ///
	if drug3!="" &drug4==""
gen share_overall3_p2 = (responders2 + responders3) / (no_randomised2 + no_randomised3) ///
	if drug3!="" &drug4==""
gen z_overall3_p1 = (share_respond1 - share_respond3) / sqrt(share_overall3_p1*(1-share_overall3_p1)* ///
	((1/no_randomised1) + (1/no_randomised3))) if drug3!="" &drug4==""
gen z_overall3_p2 = (share_respond2 - share_respond3) / sqrt(share_overall3_p1*(1-share_overall3_p1)* ///
	((1/no_randomised2) + (1/no_randomised3))) if drug3!="" &drug4==""
gen z_overall3_p3=0 if share_respond3!=. & drug3!="" &drug4==""

gen share_overall4_p1 = (responders1 + responders4) / (no_randomised1 + no_randomised4) ///
	if drug4!=""
gen share_overall4_p2 = (responders2 + responders4) / (no_randomised2 + no_randomised4) ///
	if drug4!=""
gen share_overall4_p3 = (responders3 + responders4) / (no_randomised3 + no_randomised4) ///
	if drug4!=""
gen z_overall4_p1 = (share_respond1 - share_respond4) / sqrt(share_overall4_p1*(1-share_overall4_p1)* ///
	((1/no_randomised1) + (1/no_randomised4))) if drug4!=""
gen z_overall4_p2 = (share_respond2 - share_respond4) / sqrt(share_overall4_p1*(1-share_overall4_p1)* ///
	((1/no_randomised2) + (1/no_randomised4))) if drug4!=""	
gen z_overall4_p3 = (share_respond3 - share_respond4) / sqrt(share_overall4_p1*(1-share_overall4_p1)* ///
	((1/no_randomised3) + (1/no_randomised4))) if drug4!=""
gen z_overall4_p4 =0 if share_respond4!=. & drug4!=""
	
gen z_sharerespond1 = z_overall2_p1
replace z_sharerespond1 = z_overall3_p1 if z_overall3_p1!=.
replace z_sharerespond1 = z_overall4_p1 if z_overall4_p1!=.
gen z_sharerespond2 = z_overall2_p2
replace z_sharerespond2 = z_overall3_p2 if z_overall3_p2!=.
replace z_sharerespond2 = z_overall4_p2 if z_overall4_p2!=.
gen z_sharerespond3 = z_overall3_p3
replace z_sharerespond3 = z_overall4_p3 if z_overall4_p3!=.
gen z_sharerespond4 = z_overall4_p4
drop share_overall* z_overall*

*Generate significance score for percent decrease
gen z_overall2_p1 = (effect1 - effect2) / sqrt((sd1^2/no_randomised1) + (sd2^2 / no_randomised2))
gen z_overall2_p2 = (effect2 - effect1) / sqrt((sd1^2/no_randomised1) + (sd2^2 / no_randomised2))
gen z_overall3_p1 = (effect1 - effect3) / sqrt((sd1^2/no_randomised1) + (sd3^2 / no_randomised3)) ///
	if drug3!="" &drug4==""
gen z_overall3_p2 = (effect2 - effect3) / sqrt((sd2^2/no_randomised2) + (sd3^2 / no_randomised3)) ///
	if drug3!="" &drug4==""
gen z_overall3_p3=0 if effect3!=. & drug3!="" &drug4==""
gen z_overall4_p1 =(effect1 - effect4) / sqrt((sd1^2/no_randomised1) + (sd4^2 / no_randomised4)) ///
	if drug4!=""
gen z_overall4_p2 =(effect2 - effect4) / sqrt((sd2^2/no_randomised2) + (sd4^2 / no_randomised4)) ///
	if drug4!=""	
gen z_overall4_p3 =(effect3 - effect4) / sqrt((sd3^2/no_randomised3) + (sd4^2 / no_randomised4)) ///
	if drug4!=""	
gen z_overall4_p4=0 if effect4!=. & drug4!=""	
gen z_pereffect1 = z_overall2_p1
replace z_pereffect1 = z_overall3_p1 if z_overall3_p1!=.
replace z_pereffect1 = z_overall4_p1 if z_overall4_p1!=.
gen z_pereffect2 = z_overall2_p2
replace z_pereffect2 = z_overall3_p2 if z_overall3_p2!=.
replace z_pereffect2 = z_overall4_p2 if z_overall4_p2!=.
gen z_pereffect3=z_overall3_p3
replace z_pereffect3 = z_overall4_p3 if z_overall4_p3!=.
gen z_pereffect4 = z_overall4_p4
drop z_overall*

gen placebo_control = drug1 == "placebo" | drug2=="placebo" | drug3=="placebo" | drug4 == "placebo"
forvalues n=1/4{
	gen sig5_sharerespond`n' = z_sharerespond`n' >=1.96 & !missing(z_sharerespond`n') & placebo_control ==0
	replace sig5_sharerespond`n' = z_sharerespond`n' >=1.64 & !missing(z_sharerespond`n') & placebo_control ==1
	replace sig5_sharerespond`n' = . if missing(z_sharerespond`n')	

	gen sig10_sharerespond`n' = z_sharerespond`n' >=1.64 & !missing(z_sharerespond`n') & placebo_control ==0
	replace sig10_sharerespond`n' = z_sharerespond`n' >=1.282 & !missing(z_sharerespond`n') & placebo_control ==1
	replace sig10_sharerespond`n' = . if missing(z_sharerespond`n')	
	
	gen sig5_pereffect`n' = z_pereffect`n'<=-1.96 & !missing(z_pereffect`n') & placebo_control ==0
	replace sig5_pereffect`n' = z_pereffect`n'<=-1.64 & !missing(z_pereffect`n') & placebo_control ==1
	replace sig5_pereffect`n' = . if missing(z_pereffect`n')
	
	gen sig10_pereffect`n' = z_pereffect`n'<=-1.64 & !missing(z_pereffect`n') & placebo_control ==0
	replace sig10_pereffect`n' = z_pereffect`n'<=-1.282 & !missing(z_pereffect`n') & placebo_control ==1
	replace sig10_pereffect`n' = . if missing(z_pereffect`n')
}
keep study_no drug* z_* sig*
reshape long z_sharerespond z_pereffect sig5_sharerespond sig10_sharerespond sig5_pereffect sig10_pereffect drug, ////
	i(study_no) j(num_drug)
drop if drug==""
save ../derived/Antidepressants/significance.dta, replace

/**********
* Final Datasets
***********/

use ../derived/Antidepressants/antidepressant.dta, clear
merge 1:1 study_no num_drug using ../derived/Antidepressants/significance.dta, keepusing(z_* sig*) ///
	assert(1 3) keep(1 3) nogen

destring female, replace force
rename female share_female
destring meanage, replace force
rename meanage mean_age
format studyname %50s
drop study_merge
destring lengthoftrialweeks, replace force
rename lengthoftrialweeks lengthoftrial
destring dropouts_total, replace force
destring meanbaselineseverity, replace force
gen sample = "anti"
save ../derived/Antidepressants/antidepressant.dta, replace


*Create list of unique drug combos
use ../derived/Antidepressants/antidepressant.dta, clear
keep study_no drug
duplicates drop
replace drug="zplacebo" if drug=="placebo"
bys study_no (drug): gen n=_n
replace drug="placebo" if drug=="zplacebo"
reshape wide drug, i(study_no) j(n)
bys drug*: gen temp = _n==1
gen drug_combo_no = sum(temp)
drop temp
save ../derived/Antidepressants/antidepressant_drug_combos.dta, replace

cap erase ../derived/Antidepressants/addl_arms.dta
cap erase ../derived/Antidepressants/addl_arms_chars.dta
cap erase ../derived/Antidepressants/merge_dosage.dta
cap erase ../derived/Antidepressants/merged_char_results.dta
cap erase ../derived/Antidepressants/significance.dta
cap erase ../derived/Antidepressants/studychars.dta
cap erase ../derived/Antidepressants/studychars_nodosage.dta
cap erase ../derived/Antidepressants/studyresults.dta